﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using Crochet.Configuration;
using Crochet.Core.Attributes;
using System.Collections.Generic;
using Crochet.Core.Policy;

namespace Crochet.Core
{
	public class CrochetManager : IScheduler, IDisposable
	{
		private static CrochetManager cmInstance = null;

		private Dictionary<SchedulerType, Dictionary<string, IScheduler>> dSchedulers;

		public CrochetManager()
		{
			dSchedulers = new Dictionary<SchedulerType, Dictionary<string, IScheduler>>();
		}

		public static CrochetManager Instance
		{
			get
			{
				if (cmInstance == null)
				{
					lock (typeof(CrochetManager))
					{
						if (cmInstance == null)
						{
							cmInstance = new CrochetManager();
						}
					}
				}

				return cmInstance;
			}
		}

		#region IScheduler Members

		public IExecutionPolicy CloneableExecutionPolicy
		{
			get
			{
				throw new NotSupportedException();
			}
		}

		public void Schedule(IExecutionPolicy epExecutionPolicy)
		{
			SchedulerAttribute saSchedulerAttribute = ((SchedulerAttribute[])epExecutionPolicy.GetType().GetCustomAttributes(typeof(SchedulerAttribute), true))[0];

			Schedule(saSchedulerAttribute.Type, saSchedulerAttribute.Name, epExecutionPolicy);
		}

		public void Schedule(SchedulerDelegate sdSchedulerDelegate)
		{
			Schedule(sdSchedulerDelegate, null);
		}

		public void Schedule(SchedulerDelegate sdSchedulerDelegate, ExceptionHandlerDelegate ehdExceptionHandlerDelegate)
		{
			SchedulerAttribute saSchedulerAttribute = ((SchedulerAttribute[])sdSchedulerDelegate.Method.GetCustomAttributes(typeof(SchedulerAttribute), true))[0];

			Schedule(saSchedulerAttribute.Type, saSchedulerAttribute.Name, sdSchedulerDelegate, ehdExceptionHandlerDelegate);
		}

		#endregion

		public void Schedule(SchedulerType stSchedulerType, string sName, IExecutionPolicy epExecutionPolicy)
		{
			GetScheduler(stSchedulerType, sName).Schedule(epExecutionPolicy);
		}

		public void Schedule(SchedulerType stSchedulerType, string sName, SchedulerDelegate sdSchedulerDelegate)
		{
			Schedule(stSchedulerType, sName, sdSchedulerDelegate, null);
		}

		public void Schedule(SchedulerType stSchedulerType, string sName, SchedulerDelegate sdSchedulerDelegate, ExceptionHandlerDelegate ehdExceptionHandlerDelegate)
		{
			IScheduler sScheduler = GetScheduler(stSchedulerType, sName);

			sScheduler.Schedule(sScheduler.CloneableExecutionPolicy.Clone(sdSchedulerDelegate, ehdExceptionHandlerDelegate));
		}

		public IScheduler GetScheduler(SchedulerType stSchedulerType, string sName)
		{
			IScheduler sScheduler = null;

			if (dSchedulers.ContainsKey(stSchedulerType) && dSchedulers[stSchedulerType].ContainsKey(sName))
			{
				sScheduler = dSchedulers[stSchedulerType][sName];
			}
			else
			{
				lock (cmInstance)
				{
					if (dSchedulers.ContainsKey(stSchedulerType) && dSchedulers[stSchedulerType].ContainsKey(sName))
					{
						sScheduler = dSchedulers[stSchedulerType][sName];
					}
					else
					{
						Dictionary<string, IScheduler> dSchedulerByType = null;

						if (dSchedulers.ContainsKey(stSchedulerType))
						{
							dSchedulerByType = dSchedulers[stSchedulerType];
						}
						else
						{
							dSchedulerByType = new Dictionary<string, IScheduler>();

							dSchedulers.Add(stSchedulerType, dSchedulerByType);
						}

						sScheduler = SchedulerFactory.Instance.CreateScheduler(stSchedulerType, sName);

						dSchedulerByType.Add(sName, sScheduler);
					}
				}
			}

			return sScheduler;
		}

		public IScheduler CreateScheduler(SchedulerType stSchedulerType, params object[] oaParams)
		{
			IScheduler						sScheduler			= null;
			Dictionary<string, IScheduler>	dSchedulerByType	= null;

			if (dSchedulers.ContainsKey(stSchedulerType))
			{
				dSchedulerByType = dSchedulers[stSchedulerType];
			}
			else
			{
				dSchedulerByType = new Dictionary<string, IScheduler>();

				dSchedulers.Add(stSchedulerType, dSchedulerByType);
			}

			sScheduler = SchedulerFactory.Instance.CreateScheduler(stSchedulerType, oaParams);

			dSchedulerByType.Add((string)oaParams[0], sScheduler);

			return sScheduler;
		}

		#region IDisposable Members

		public void Dispose()
		{
			if (dSchedulers.Count > 0)
			{
				foreach (Dictionary<string, IScheduler> dSchedulerType in dSchedulers.Values)
				{
					if (dSchedulerType.Count > 0)
					{
						foreach (IScheduler sScheduler in dSchedulerType.Values)
						{
							sScheduler.Dispose();
						}
					}
				}
			}

			dSchedulers	= null;
			cmInstance	= null;
		}

		#endregion
	}
}
